package tool.plot

import implicits.Implicits._
import play.api.data.FormBinding
import play.api.mvc._
import shared.Pojo._
import shared.pojo.mission
import tool.Tool
import tool.plot.AllPlot.MyRPlotB
import upickle.default._
import utils.Utils

import java.io.File

/** Created by yz on 27/9/2020
 */
object CorHeatmapPlot extends CorHeatmapPlotT with MyRPlotB {

  val plotO = mission.CorHeatmapPlot

  def getData(implicit request: Request[AnyContent], formBinding: FormBinding) = {
    Tool.transformRq.transform(reader[plotO.Request])
  }

  override def getWorkspaceDir(workspaceDir: File, idStr: String,prefix:String=""): File = {
    new File(workspaceDir, s"${idStr}")
  }

  def producePlotConfigFile(workspaceDir: File)(
    implicit
    request: Request[AnyContent],
    formBinding: FormBinding
  ) = {
    val data = getData
    Utils.t2ConfigFileWithDefault(data, configFile(workspaceDir, data.idStr))
  }

  def configFile(workspaceDir: File, prefix: String) = {
    val execWorkspaceDir = getWorkspaceDir(workspaceDir, prefix)
    new File(execWorkspaceDir, plotO.configFileName)
  }

  override def getCommand(implicit configJson: ujson.Value, workspaceDir: File): String = {
    val data = configJson.transform(reader[plotO.Request])
    val (layout, color, font) = (data.layout, data.color, data.font)
    s"""
       |${"Rscript".condaPath()} ${new File(Tool.rPath, "fs_cor_heatmap.R").unixPath} --pc ${plotO.configFileName}
       |convert  -density 300 Correlation_Heatmap.pdf Correlation_Heatmap.png
       |""".stripMargin
  }

  def getCommand(workspaceDir: File, idStr: String): String = {
    implicit val configJson = ujson.read(configFile(workspaceDir, idStr))
    implicit val wd = workspaceDir
    getCommand
  }

  def initConfigFile(workspaceDir: File, idStr: String) = {
    val configData = plotO
      .Request()
    Utils.t2ConfigFileWithDefault(configData, configFile(workspaceDir, idStr))
  }

}
